<?php

namespace app\common\lib;

use support\Redis;

class RedisCache
{
    /**
     * 获取缓存
     * @param $key
     * @param int $num
     * @return mixed
     */
    public static function get($key, $num = 0)
    {
        Redis::select($num);
        return json_decode(Redis::get($key), true);
    }

    /**
     * 设置缓存
     * @param $key
     * @param array $data
     * @param int $expiresTime
     * @param int $num
     * @return bool|int
     */
    public static function set($key, $data = [], $expiresTime = 0, $num = 0)
    {
        Redis::select($num);
        if ([] === $data || is_null($data)) {
            // 删除缓存
            return Redis::del($key);
        }
        $res = Redis::set($key, json_encode($data));
        if ($expiresTime > 0) {
            Redis::expire($key, $expiresTime);
        }
        return $res;
    }

    /**
     * 未找到方法时调用
     * @param $name
     * @param $args
     * @return mixed
     */
    public static function __callStatic($name, $args)
    {
        Redis::select($args[0][1]);
        return Redis::$name(...$args[0][0]);
    }
}